package com.cfpamf.athean.rabbit.mq.plus.repository.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfpamf.athean.rabbit.mq.plus.common.constant.BusinessConstants;
import com.cfpamf.athean.rabbit.mq.plus.common.enums.ProcessStatusEnum;
import com.cfpamf.athean.rabbit.mq.plus.domain.po.RabbitMqPlusEventTaskPO;
import com.cfpamf.athean.rabbit.mq.plus.mapper.RabbitMqPlusEventTaskMapper;
import com.cfpamf.athean.rabbit.mq.plus.repository.RabbitMqPlusEventTaskRepository;
import com.cfpamf.athena.utils.Assert;
import com.cfpamf.athena.utils.DateUtils;
import com.cfpamf.athena.utils.Tools;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Andy（陈洪桥）
 * @since 2022-11-26 15:15:40
 */
@Slf4j
@Service
public class RabbitMqPlusEventTaskRepositoryImpl extends
    ServiceImpl<RabbitMqPlusEventTaskMapper, RabbitMqPlusEventTaskPO> implements RabbitMqPlusEventTaskRepository {

    @Resource
    private RabbitMqPlusEventTaskMapper rabbitMqPlusEventTaskMapper;

    @Override
    public RabbitMqPlusEventTaskPO selectOneByEventNoAndEventCode(String eventNo, String eventTypeCode) {
        if (Tools.isEmpty(eventNo)) {
            return null;
        }
        if (Tools.isEmpty(eventTypeCode)) {
            return null;
        }
        LambdaQueryWrapper<RabbitMqPlusEventTaskPO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RabbitMqPlusEventTaskPO::getEventNo, eventNo);
        queryWrapper.eq(RabbitMqPlusEventTaskPO::getEventTypeCode, eventTypeCode);
        return rabbitMqPlusEventTaskMapper.selectOne(queryWrapper);
    }
    
    
    public int countByEventNoAndEventCode(String eventNo, String eventTypeCode) {
        if (Tools.isEmpty(eventNo)) {
            throw new RuntimeException("eventNo不能为空！");
        }
        if (Tools.isEmpty(eventTypeCode)) {
            throw new RuntimeException("eventTypeCode不能为空！");
        }
        LambdaQueryWrapper<RabbitMqPlusEventTaskPO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RabbitMqPlusEventTaskPO::getEventNo, eventNo);
        queryWrapper.eq(RabbitMqPlusEventTaskPO::getEventTypeCode, eventTypeCode);
        return rabbitMqPlusEventTaskMapper.selectCount(queryWrapper);
    }

    @Override
    public List<RabbitMqPlusEventTaskPO> queryTodoDataByStatus(List<Integer> status) {
        if (Tools.isEmpty(status)) {
            return null;
        }
        LambdaQueryWrapper<RabbitMqPlusEventTaskPO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(RabbitMqPlusEventTaskPO::getStatusCode, status);
        queryWrapper.gt(RabbitMqPlusEventTaskPO::getMaxRetryCount, BusinessConstants.ZERO);
        queryWrapper.le(RabbitMqPlusEventTaskPO::getNextExecuteTime, DateUtils.format(DateUtils.nowTime()));
        return rabbitMqPlusEventTaskMapper.selectList(queryWrapper);
    }

    @Override
    public boolean updateByEventNoAndEventCode(RabbitMqPlusEventTaskPO po) {
        Assert.notEmpty(po, "抱歉，更新事件任务对象条件为空，拒绝更新！");
        Assert.notEmpty(po.getEventNo(), "抱歉，更新事件任务对象条件eventNo为空，拒绝更新！");
        Assert.notEmpty(po.getEventTypeCode(), "抱歉，更新事件任务对象条件eventTypeCode为空，拒绝更新！");
        LambdaUpdateWrapper<RabbitMqPlusEventTaskPO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(RabbitMqPlusEventTaskPO::getEventNo, po.getEventNo());
        updateWrapper.eq(RabbitMqPlusEventTaskPO::getEventTypeCode, po.getEventTypeCode());
        updateWrapper.ne(RabbitMqPlusEventTaskPO::getStatusCode, ProcessStatusEnum.DONE.getCode());
        return super.update(po, updateWrapper);
    }

    @Override
    public boolean deleteInvalidDataByDay(int day) {
        if (log.isDebugEnabled()) {
            log.debug("[rabbitMqPlus]请检查 rabbitMqPlus.delete.invalid.data.day=[{}] 配置值小于=10，无效数据至少保留10天以上！", day);
        }
        Assert.isTrue(day >= 10, "抱歉，请检查 abbitMqPlus.delete.invalid.data.day 配置值小于=10，无效数据至少保留10天以上！");
        Date deleteInvalidDataByDayTime = DateUtils.addDay(day);
        LambdaQueryWrapper<RabbitMqPlusEventTaskPO> deleteWrapper = Wrappers.lambdaQuery();
        deleteWrapper.eq(RabbitMqPlusEventTaskPO::getStatusCode, ProcessStatusEnum.DONE.getCode());
        deleteWrapper.gt(RabbitMqPlusEventTaskPO::getEventTime, deleteInvalidDataByDayTime);
        int total = rabbitMqPlusEventTaskMapper.deleteByLimit5000(deleteWrapper);
        log.info("[rabbitMqPlus] {}天，无效数据删除条数:{}", DateUtils.format(deleteInvalidDataByDayTime), total);
        return Boolean.TRUE;
    }

}
